<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="退单号" prop="channelRefundNo">
        <el-input v-model="queryParams.channelRefundNo" placeholder="请输入退单号" clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="channelOrderNo">
        <el-input v-model="queryParams.channelOrderNo" placeholder="请输入订单号" clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户订单号" prop="merchantOrderNo">
        <el-input v-model="queryParams.merchantOrderNo" placeholder="请输入商户单订单号" clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退单类型" prop="channelType">
        <el-select v-model="queryParams.channelType" placeholder="请选择退单类型" filterable clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.pc_channel_type)"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="渠道用户名称" prop="channelUserName" label-width="120px">
        <el-input v-model="queryParams.channelUserName" placeholder="请输入渠道用户名称" clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退单状态" prop="channelStatus">
        <el-select v-model="queryParams.channelStatus" placeholder="请选择退单状态" filterable clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.pc_channel_status)"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择支付状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.pc_pay_status)"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退款时间" prop="refundTime">
        <el-date-picker v-model="queryParams.refundTime" style="width: 320px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item label="退货发货时间" prop="receivingTime" label-width="100px">
        <el-date-picker v-model="queryParams.receivingTime" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['pekingfish:channelOrderRefund:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" border :max-height="tableHeight" :data="list">
      <!--    1.文字颜色
  柔和灰（text-muted）
  主要蓝（text-primary）
  次要灰（text-secondary）
  成功绿（text-success）
  危险红（text-danger）
  警告黄（text-warning）
  危息绿（text-info）
  黑白对比（text-dark）
-->
      <el-table-column label="操作" align="center" width="100" fixed="left" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button v-if="scope.row.channelStatusRefund === -1" type="success" size="mini" plain icon="el-icon-check"
                     @click="handleConsent(scope.row,scope.index)"
                     v-hasPermi="['pekingfish:channelOrderRefund:consent']"
          >同意
          </el-button>
          <el-button v-if="scope.row.channelStatusRefund === -1" size="mini" type="secondary" icon="el-icon-close"
                     @click="handleTurnDown(scope.row,scope.index)"
                     v-hasPermi="['pekingfish:channelOrderRefund:turnDown']"
          >驳回
          </el-button>
          <el-button v-if="scope.row.channelStatusRefund === 2&&scope.row.channelType === 5" size="mini" type="danger"
                     icon="el-icon-house"
                     @click="handleTakeDelivery(scope.row,scope.index)"
                     v-hasPermi="['pekingfish:channelOrderRefund:takeDelivery']"
          >收货
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row,scope.index)"
                     v-hasPermi="['pekingfish:channelOrderRefund:detail']"
          >订单详细
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="自增主键" v-if="false" align="center" prop="id"/>
      <el-table-column label="退单号" align="center" prop="channelRefundNo"/>
      <el-table-column label="订单号" align="center" prop="channelOrderNo"/>
      <el-table-column label="商户订单号" align="center" prop="merchantOrderNo" width="150" fixed="left"/>
      <el-table-column label="退单类型" align="center" prop="channelType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.pc_channel_type" :value="scope.row.channelType"/>
        </template>
      </el-table-column>
      <el-table-column label="渠道用户名称" align="center" prop="channelUserName"/>
      <el-table-column label="联系方式" align="center" prop="channelUserPhone"/>
      <el-table-column label="退单状态" align="center" prop="channelStatusRefund">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.pc_channel_status" :value="scope.row.channelStatusRefund"/>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.pc_pay_status" :value="scope.row.payStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="手机品牌" align="center" prop="brand"/>
      <el-table-column label="手机型号" align="center" prop="model"/>
      <el-table-column label="型号类型" align="center" prop="modelType"/>
      <el-table-column label="订单金额" align="center" prop="originalPrice"/>
      <el-table-column label="预付款" align="center" prop="advancePrice"/>
      <el-table-column label="尾款" align="center" prop="trailPrice"/>
      <el-table-column label="退款金额" align="center" prop="refundPrice"/>
      <el-table-column label="订单时间" align="center" prop="channelOrderTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.channelOrderTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.refundTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退货发货时间" align="center" prop="receivingTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.receivingTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退货物流单号" align="center" prop="trackingNumber"/>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />

    <!-- 订单详情 -->
    <el-dialog title="订单详情" :visible.sync="openDetail" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="30">
            <el-form-item label="品牌：">{{ form.brand }}</el-form-item>
          </el-col>
          <el-col :span="30">
            <el-form-item label="手机型号：">{{ form.model }}</el-form-item>
          </el-col>
          <el-col :span="30">
            <el-form-item label="型号类型：">{{ form.modelType }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="IMEI：">{{ form.imei }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参数：">{{ form.memory }}，{{ form.colour }}，{{ form.buy }}，{{
                form.network
              }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="机况：">{{ form.subrackDesc }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片：">
              <imageUpload v-model="form.reportImg"/>
            </el-form-item>
          </el-col>


          <el-col :span="24" v-if="form.refundImg!=null">
            <el-form-item label="凭证图片：">
              <imageUpload v-model="form.refundImg"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.refundVideo!=null">
            <el-form-item label="凭证视频：">
              <VideoUpload v-model="form.refundVideo"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openDetail = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报价单ID" prop="reportId">
          <el-input v-model="form.reportId" placeholder="请输入报价单ID"/>
        </el-form-item>
        <el-form-item label="渠道用户" prop="channelUserId">
          <el-input v-model="form.channelUserId" placeholder="请输入渠道用户"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="channelUserPhone">
          <el-input v-model="form.channelUserPhone" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="退单类型" prop="channelType">
          <el-select v-model="form.channelType" placeholder="请选择退单类型">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="退单号" prop="channelRefundNo">
          <el-input v-model="form.channelRefundNo" placeholder="请输入退单号"/>
        </el-form-item>
        <el-form-item label="订单号" prop="channelOrderNo">
          <el-input v-model="form.channelOrderNo" placeholder="请输入订单号"/>
        </el-form-item>
        <el-form-item label="商户订单号" prop="merchantOrderNo">
          <el-input v-model="form.merchantOrderNo" placeholder="请输入商户单订单号"/>
        </el-form-item>
        <el-form-item label="支付状态" prop="payStatus">
          <el-radio-group v-model="form.payStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="退单状态" prop="channelStatus">
          <el-radio-group v-model="form.channelStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="回收价" prop="recovery">
          <el-input v-model="form.recovery" placeholder="请输入回收价"/>
        </el-form-item>
        <el-form-item label="退款金额" prop="refundPrice">
          <el-input v-model="form.refundPrice" placeholder="请输入退款金额"/>
        </el-form-item>
        <el-form-item label="订单时间" prop="channelOrderTime">
          <el-date-picker clearable v-model="form.channelOrderTime" type="date" value-format="timestamp"
                          placeholder="选择订单时间"
          />
        </el-form-item>
        <el-form-item label="退款时间" prop="refundTime">
          <el-date-picker clearable v-model="form.refundTime" type="date" value-format="timestamp"
                          placeholder="选择退款时间"
          />
        </el-form-item>
        <el-form-item label="退货发货时间" prop="receivingTime">
          <el-date-picker clearable v-model="form.receivingTime" type="date" value-format="timestamp"
                          placeholder="选择退款发货时间"
          />
        </el-form-item>
        <el-form-item label="快递名称" prop="trackingName">
          <el-input v-model="form.trackingName" placeholder="请输入快递名称"/>
        </el-form-item>
        <el-form-item label="物流单号" prop="trackingNumber">
          <el-input v-model="form.trackingNumber" placeholder="请输入物流单号"/>
        </el-form-item>
        <el-form-item label="审核状态" prop="checkStatus">
          <el-radio-group v-model="form.checkStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="checkRemark">
          <el-input v-model="form.checkRemark" placeholder="请输入审核备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 同意 -->
    <el-dialog :title="title" :visible.sync="openConsent" width="500px" v-dialogDrag append-to-body>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormOpenConsent">确 定</el-button>
        <el-button @click="cancelOpenConsent">取 消</el-button>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="申请退款金额：">{{ form.refundPrice }}</el-form-item>
        <el-form-item label="退款原因：">{{ form.refundDesc }}</el-form-item>
        <el-form-item label="驳回凭证图片" prop="refundImg">
          <imageUpload v-model="form.refundImg"/>
        </el-form-item>

        <el-form-item label="驳回凭证视频" prop="refundVideo">
          <VideoUpload v-model="form.refundVideo"/>
        </el-form-item>
        <el-form-item label="退款金额" prop="refundPriceConsent">
          <el-input-number v-model="form.refundPriceConsent" :min="0" :max="form.refundPrice"
                           :precision="2"></el-input-number>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 驳回 -->
    <el-dialog :title="title" :visible.sync="openTurnDown" width="500px" v-dialogDrag append-to-body>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormOpenTurnDown">确 定</el-button>
        <el-button @click="cancelOpenTurnDown">取 消</el-button>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="申请退款金额：">{{ form.refundPrice }}</el-form-item>
        <el-form-item label="退款原因：">{{ form.refundDesc }}</el-form-item>
        <el-form-item label="退款凭证图片" prop="refundImg">
          <imageUpload v-model="form.refundImg"/>
        </el-form-item>

        <el-form-item label="退款凭证视频" prop="refundVideo">
          <VideoUpload v-model="form.refundVideo"/>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  create,
  update,
  get,
  getPage,
  exportExcel, turnDownRefund, consentRefund, takeDelivery,
} from '@/api/pekingfish/appChannelOrderRefund'
import ImageUpload from '@/components/ImageUpload'
import ImagePreview from '@/components/ImagePreview'
import VideoUpload from "@/components/VideoUpload";
import Editor from "@/components/Editor";

export default {
  name: '',
  components: {
    VideoUpload, Editor,
    ImageUpload, ImagePreview,
  },
  data() {
    return {
      tableHeight: document.documentElement.scrollHeight - 245 + "px",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 渠道单退单列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openTurnDown: false,
      openConsent: false,
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        reportId: null,
        channelUserId: null,
        channelUserPhone: null,
        channelType: null,
        channelRefundNo: null,
        channelOrderNo: null,
        merchantOrderNo: null,
        payStatus: null,
        channelStatus: null,
        recovery: null,
        refundPrice: null,
        createTime: [],
        channelOrderTime: [],
        refundTime: [],
        receivingTime: [],
        trackingName: null,
        trackingNumber: null,
        checkStatus: null,
        checkRemark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merchantOrderNo: [{required: true, message: '商户单订单号不能为空', trigger: 'blur'}]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        reportId: undefined,
        channelUserName: undefined,
        channelUserPhone: undefined,
        channelType: undefined,
        channelRefundNo: undefined,
        channelOrderNo: undefined,
        merchantOrderNo: undefined,
        payStatus: undefined,
        channelStatus: undefined,
        recovery: undefined,
        refundPrice: undefined,
        channelOrderTime: undefined,
        refundTime: undefined,
        receivingTime: undefined,
        trackingName: undefined,
        trackingNumber: undefined,
        checkStatus: undefined,
        checkRemark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.openDetail = true
      this.form = row
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加渠道单退单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      get(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改渠道单退单'
      })
    },
    /** 收货 */
    handleTakeDelivery(row) {
      const channelRefundNo = row.channelRefundNo
      this.$modal.confirm('是否确认同意退单单编号为"' + channelRefundNo + '"的数据收货?').then(function () {
        return takeDelivery(row.id, row.merchantOrderNo)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('收货成功')
      }).catch(() => {
      })
    },
    /** 同意操作 */
    handleConsent(row) {
      this.reset()
      const id = row.id
      get(id).then(response => {
        this.form = response.data
        this.openConsent = true
        this.title = '同意'
      })
    },
    /** 同意提交按钮 */
    submitFormOpenConsent() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        consentRefund(this.form).then(response => {
          this.$modal.msgSuccess('操作成功')
          this.openConsent = false
          this.getList()
        })

      })
    },

    /** 驳回取消按钮 */
    cancelOpenTurnDown() {
      this.openTurnDown = false
      this.reset()
    },

    /** 驳回取消按钮 */
    cancelOpenConsent() {
      this.openConsent = false
      this.reset()
    },
    /** 驳回操作 */
    handleTurnDown(row) {
      this.reset()
      const id = row.id
      get(id).then(response => {
        this.form = response.data
        this.openTurnDown = true
        this.title = '驳回'
      })
    },
    /** 驳回提交按钮 */
    submitFormOpenTurnDown() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        turnDownRefund(this.form).then(response => {
          this.$modal.msgSuccess('驳回成功')
          this.openTurnDown = false
          this.getList()
        })

      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        // 修改的提交
        if (this.form.id != null) {
          update(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
          return
        }
        // 添加的提交
        create(this.form).then(response => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.getList()
        })
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams}
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal.confirm('是否确认导出所有渠道单退单数据项?').then(() => {
        this.exportLoading = true
        return exportExcel(params)
      }).then(response => {
        this.$download.excel(response, '渠道单退单.xls')
        this.exportLoading = false
      }).catch(() => {
      })
    }
  }
}
</script>
